//
//  XQToast.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQToast.h"
#import "UIView+Toast.h"
#import "UIApplication+DDAdd.h"
#import "UIView+DDAdd.h"

@implementation XQToast

+ (instancetype)toast {
    static XQToast *toast = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        toast = [[XQToast alloc] init];
    });
    return toast;
}

+ (void)showMsg:(NSString *)msg {
    [[self toast] showMsg:msg];
}

+ (void)showMsg:(NSString *)msg image:(UIImage *)image {
    [[self toast] showMsg:msg image:image duration:2];
}

+ (void)showMsg:(NSString *)msg duration:(CGFloat)duration {
    [[self toast] showMsg:msg image:nil duration:duration];
}

+ (void)showMsg:(NSString *)msg padding:(CGFloat)padding {
    
    if (!msg || msg.length == 0) {
        return;
    }
    UIWindow *window = [[UIApplication sharedExtensionApplication] keyWindow];
    
    CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle];
    style.backgroundColor = [[UIColor colorWithHexString:@"1E232B"] colorWithAlphaComponent:0.9];
    style.messageFont = [UIFont systemFontOfSize:14.f];
    style.messageColor = [UIColor whiteColor];
    style.verticalTitleImagePadding = 8;
    style.horizontalPadding = 20.0;
    style.verticalPadding = 10.0;
    style.maxWidthPercentage = 0.8;
    style.cornerRadius = 4;
    style.messageAlignment = NSTextAlignmentCenter;
    style.titleAlignment = NSTextAlignmentCenter;
    
    [window makeKeyAndVisible];
    [CSToastManager setTapToDismissEnabled:NO];
    
    [window makeToast:msg
             duration:2.f
             position:[NSValue valueWithCGPoint:(CGPoint){window.width/2, padding}]
                title:nil
                image:nil
                style:style
           completion:nil
        allCompletion:nil];
}

#pragma mark - Private Method

- (void)showMsg:(NSString *)msg {
    [self showMsg:msg image:nil duration:2];
}

- (void)showMsg:(NSString *)msg image:(UIImage *)image duration:(CGFloat)duration {
    if (!msg || msg.length == 0) {
        return;
    }
    UIWindow *window = [[UIApplication sharedExtensionApplication] keyWindow];
    
    CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle];
    style.backgroundColor = [[UIColor colorWithHexString:@"1E232B"] colorWithAlphaComponent:0.9];
    style.messageFont = [UIFont systemFontOfSize:14.f];
    style.messageColor = [UIColor whiteColor];
    style.verticalTitleImagePadding = 8;
    style.horizontalPadding = 20.0;
    style.verticalPadding = 10.0;
    style.maxWidthPercentage = 0.8;
    style.cornerRadius = 4;
    style.messageAlignment = NSTextAlignmentCenter;
    style.titleAlignment = NSTextAlignmentCenter;
    if (image) {
        style.imageSize = image.size;
        style.verticalPadding = 20.0;
    }
    
    [window makeKeyAndVisible];
    [CSToastManager setTapToDismissEnabled:NO];
    
    [window makeToast:msg
             duration:duration
             position:[NSValue valueWithCGPoint:(CGPoint){window.width/2, window.height / 2}]
                title:nil
                image:image
                style:style
           completion:nil
        allCompletion:nil];
}

@end
